<template>
  <div class="login-box">
    <div class="header"></div>
    <el-container class="juzhong">
      <el-header>用户登录</el-header>
      <el-form :model="ruleform" :rules="rules" ref="ruleform" status-icon>
        <el-form-item prop="username">
          <el-input type="text"
            placeholder="请输入账号"
            v-model="ruleform.username">
            <i slot="prefix" class="el-input__icon el-icon-user-solid"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            type="password"
            v-model="ruleform.password"
          >
            <i slot="prefix" class="el-input__icon el-icon-view"></i>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit('ruleform')">提交</el-button>
          <el-button @click="reset('ruleform')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-container>
  </div>
</template>


<script>
import { mapActions } from "vuex";
export default {
  data() {
    return {
      ruleform: {
        password: "",
        username: "",
      },
      // iDate {
      //   pageNum: 1,
      //   pageSize: 5,
      // },
      rules: {
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 5, max: 8, message: "长度在 5 到 8 个字符", trigger: "blur" },
        ],
      },
    };
  },
  
  methods: {
    ...mapActions(["getLogin"]),
    submit(ruleform) {
      this.$refs[ruleform].validate((valid) => {
        const loading = this.$loading({
          lock: true,
          text: "稍等....",
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.7)",
        });
        if (valid) {
          //校验成功
          this.getLogin(this.ruleform).then((a) => {
            if(!a.data.status) {
                this.$router.replace("/firstPage");
            }
          });
        } else {
          //失败
          this.$message({
            message: "输入信息有错误",
            type: "warning",
          });
        }
        loading.close();
      });
    },
    reset(ruleform) {
      this.$refs[ruleform].resetFields();
    },
  },
};
</script>
  
</style>
<style scoped>
.header {
  width: 100%;
  height: 80px;
  background-color: rgba(0, 0, 0, 0.3);
  margin-bottom: 40px;
}
.juzhong {
  margin: 0 auto;
  width: 300px;
}
</style>